package cn.txm.user.controller;

import cn.txm.basic.util.AjaxResult;
import cn.txm.basic.util.RedisUtils;
import cn.txm.org.domain.Shop;
import cn.txm.org.service.IShopService;
import cn.txm.user.domain.dto.UserDto;
import cn.txm.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

@RestController
@RequestMapping("/lr")
public class LoginAndRegisterController {

    @Autowired
    private IUserService userService;

    @Autowired
    private IShopService shopService;
    /**
     * 验证手机号是否被注册
     * type phone_reg表示手机验证码注册，phone_login表示手机验证码登录
     */
    @GetMapping("/validatePhone/{type}/{phone}")
    public AjaxResult validatePhone(@PathVariable("type") String type,@PathVariable("phone") String phone){
        return userService.validatePhone(type,phone);
    }

    /**
     * 发送手机短信
     * @param phone
     * @return
     */
    @GetMapping("/sendMobileCode/{type}/{phone}")
    public AjaxResult sendMobileCode(@PathVariable("type") String type,@PathVariable("phone") String phone){
        return userService.sendMobileCode(type,phone);
    }

    /**
     * 前台用户注册，提交注册表单
     * @param userDto
     * @return
     */
    @PutMapping("/phoneReg")
    public AjaxResult phoneReg(@RequestBody UserDto userDto){
        return userService.phoneReg(userDto);
    }

    /**
     * 商家入驻提交表单
     * @param shop
     * @return
     */
    @PostMapping("/settledIn")
    public AjaxResult settledIn(@RequestBody Shop shop){
        try {
            return shopService.settledIn(shop);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("注册失败"+e.getMessage());
        }
    }
    /**
     *
     * @param id 审核邮箱中带的id
     * @return
     */
    //<a href='http://localhost:8082/lr/shopRegActive/"+id+"'>点击激活</a>
    @GetMapping("/shopRegActive/{id}")
    public void  shopapprove(@PathVariable("id")Long id, HttpServletResponse response) {
        try {
            shopService.shopapprove(id);
            response.setContentType("text/html; charset=UTF-8");//注意text/html，和application/html
            response.getWriter().print("<html><body><script type='text/javascript'>alert('恭喜您！激活成功！');</script></body></html>");
            response.setHeader("refresh","1;http://localhost:8081");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    /**
     * 用户登录
     * @param userDto  type属性为front表示前台用户登录，admin表示后台用户登录
     * @return
     */
    @PostMapping("/userLogin")
    public AjaxResult userLogin(@RequestBody UserDto userDto){

        return userService.userLogin(userDto);
    }

    /**
     * 手机验证码登录
     * @param userDto
     * @return
     */
    @PostMapping("/phoneLogin")
    public AjaxResult phoneLogin(@RequestBody UserDto userDto){

        return userService.phoneLogin(userDto);
    }

    /**
     * 微信登录接口：获取code参数，利用Java程序发送Http请求获取access_token
     * @param code
     * @return
     */
    @GetMapping("/wechatLogin/{code}")
    public AjaxResult wechatLogin(@PathVariable("code")String code){
        return userService.wechatLogin(code);
    }

    /**
     * 微信用户绑定：通过账户密码绑定
     * @return
     */
    @PutMapping("/userBindAccount")
    public AjaxResult userBindAccount(@RequestBody UserDto userDto){
        return userService.userBindAccount(userDto);
    }


    /**
     * 退出登录
     * @param request
     * @return
     */
    @GetMapping("/logout")
    public AjaxResult logout(HttpServletRequest request){
        //获取请求头User-Token
        String userToken = request.getHeader("User-Token");
        if(StringUtils.hasText(userToken)){
            //从Redis中删除用户信息
            RedisUtils.INSTANCE.del(userToken);
        }
        return AjaxResult.me();
    }



}
